package net.bat.service.cb;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import tool.LogUtil;
import net.bat.db.BaseDAOImpl;
import net.bat.pt.BObj;
import net.bat.util.Cfg;
import net.cs.um.UMService;
import dao.hb.BxDetailrecord;
import dao.hb.BxMidle;
import dao.hb.BxSimplerecord;
import dao.hb.UUser;

public class CBBxSimplerecord implements IHandleHook {

	@Override
	public void beforeHandle(char method, HashMap<String, Object> psMap,
			HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		
		BaseDAOImpl bdi = (BaseDAOImpl) Cfg.getBean("BaseDAOImpl");
		BObj[] objs = (BObj[])psMap.get("objs");
		String special = "";
		if(psMap.containsKey("special")){
			special  = (String)psMap.get("special");
		}
		Date dt = new Date();
		UUser usr = UMService.getUserBySession(request);
		int uid = usr.getId();
		String name=usr.getName();
	
		
		for(int i=0; i<objs.length; i++){
			BObj cur = objs[i];
			HashMap<String,Object>  its = cur.getItems();
		
			if(cur.getAction()==BObj.ACTION_UPDATE){
				Object id=its.get("id");
				Object ud=its.get("uid");
				
				if(id!=null&&ud!=null){
				
					String hql="from BxDetailrecord where bxsrPid="+id.toString();
					List dList=bdi.find(hql);
					if(dList.size()>0){
						for(int j=0;j<dList.size();j++){
							BxDetailrecord bdetail=(BxDetailrecord) dList.get(j);
							bdetail.setUid(Integer.parseInt(ud.toString()));
							bdi.update(bdetail);
						}
					}
				}
				
				its.put("dtCheck", dt);
			}else if(cur.getAction()==BObj.ACTION_ADD){
				  if(its.get("recorder")==null||its.get("recorder").equals("")){
		            	its.put("recorder", name);
		            }
					
					if(its.get("uid")==null){
						its.put("uid",uid);
					}
					//2014.02.08          
					if(its.get("status")==null){
						its.put("status","已提交");
					}
					if(psMap.get("specil")!=null){
						its.put("diff","true");
					}
					
					Object ud=its.get("uid");
                    if(ud!=""&&ud!=null){
                    	String uql="from UUser where id="+ud;
    					List ul=bdi.find(uql);
    					if(ul.size()>0){
    						UUser u=(UUser)ul.get(0);
    						its.put("uname",u.getName());
    					}
                    }
					
			}

			//删除，连带删除明细表         2014.02.19 fro Clover_whd
			if(cur.getAction()==BObj.ACTION_REMOVE){
				     Integer id=(Integer)its.get("id");
				     String hql="from BxDetailrecord where bxsrPid="+id;
				     String mql="from BxMidle where bxSid="+id;
				     List li= bdi.find(hql);
				     List ml=bdi.find(mql);
				     if(li!=null &&li.size()>0){
					     for(int i1=0;i1<li.size();i1++){
						     String logdetail =  "删除批次表编号："+id.toString()+"; 删除报销申请表编号：" ;
						     BxDetailrecord bd = (BxDetailrecord) li.get(i1);
					    	 bdi.delete(li.get(i1));
					    	 LogUtil.getInstance().RecordLog(usr.getUserId(),"删除", new Date(), logdetail+bd.getId(),usr.getIpaddr(),bd.getDtLook());
					     }
				     }
				     if(ml!=null &&ml.size()>0){
					     for(int m1=0;m1<ml.size();m1++){
					    	  String logdetail =  "删除批次表编号："+id.toString()+"; 连带删除报销表编号：" ;
					    	  BxMidle bm = (BxMidle) ml.get(m1);
					    	 bdi.delete(ml.get(m1));
					    	 LogUtil.getInstance().RecordLog(usr.getUserId(),"删除", new Date(), logdetail+bm.getId(),usr.getIpaddr(),bm.getDtLook());
					    	
					     }
				     }
				     String sql="from BxSimplerecord where id="+id;
				     List sList=bdi.find(sql);
				     
				     if(sList.size()>0){
				    	 BxSimplerecord bs=(BxSimplerecord) sList.get(0);
				    	 int ui=bs.getUid();
				    	 if(bs.getSub()!=null&&bs.getSub()==1){
					    	 String uil="from UUser where id="+ui;
					    	 List ul=bdi.find(uil);
					    	 if(ul.size()>0){
					    		 UUser uler=(UUser)ul.get(0);
					    		 uler.setSubbgfFlag("true");
					    		 bdi.update(uler);
					    	 }
					     }
				     }
				     
				     
				}
			
		}
		
	}

	@Override
	public void afterHandle(char method, HashMap<String, Object> psMap,
			HttpServletRequest request, Object result) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
